<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdapterWebController;
use App\Modules\User\User;
use Illuminate\Http\Request;

class Login extends AdapterWebController
{

    /**
     * api 用户初次登录 获取token
     */
    public function adminlogin(Request $request)
    {
        # get 页面
        if ($request->isMethod('get')) {
            return view('admin.login.adminlogin');
        }

        # 提交数据
        $postData = $request->all();
        # 参数暂时放后台
        $postData['img_type'] = 'login';
        $postData['img_code_id'] = 1;
        $postData['auth_code'] = '';
        $postData['is_auto_login'] = 0;
        # post 提交
        $info = $this->appLayerExec(User::class, 'login', $postData);
        # 登录成功
        if($info['code'] == 200) {
            $request->session()->put('username', $postData['name']);
            $request->session()->put('userId', $info['res']['res']['id']);
            return redirect('/admin/adminInfo/'.$info['res']['res']['id']);
        }

       // return redirect()->route('admin/adminInfo', ['id'=>$info['res']['id']]);
    }

    /**
     * 安全退出
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout(Request $request)
    {
        session()->forget('username');
        session()->forget('userId');
        session()->forget('request_token');
        return redirect('admin/adminlogin');
    }
}